﻿using System.Collections.Generic;
using System.Linq;

namespace MedReg
{
    public partial class CardRecord
    {
        public void ImportList(List<Classes.CardRecord> CardRecordList)
        {
            var mdb = new MedregDBDataContext();

            var sPost = new Classes.Post();
            var sSub = new Classes.Subdivision();
            var sMil = new Classes.Military();
            var sMCare = new Classes.MedicalCare();
            var sMCon = new Classes.MedicalCondition();
            var sReg = new Classes.Regime();
            var sPos = new Classes.Position();
            var sPosType = new Classes.PositionType();
            var sRTI = new Classes.RecordTypeIn();
            var sRTO = new Classes.RecordTypeOut();

            var post = new Post();
            post.ImportList(sPost.getPostList(CardRecordList));
            var sub = new Subdivision();
            sub.ImportList(sSub.getSubdivisionList(CardRecordList));
            var mil = new Military();
            mil.ImportList(sMil.getMilitary(CardRecordList));
            var mCare = new MedicalCare();
            mCare.ImportList(sMCare.getMedicalCareList(CardRecordList));
            var Mcon = new MedicalCondition();
            Mcon.ImportList(sMCon.getMedicalConditionList(CardRecordList));
            var Reg = new Regime();
            Reg.ImportList(sReg.getRegimeList(CardRecordList));
            var Pos = new Position();
            Pos.ImportList(sPos.getPositionList(CardRecordList));
            var PosType = new PositionType();
            PosType.ImportList(sPosType.getPositionTypeList(CardRecordList));
            var RTI = new RecordTypeIn();
            RTI.ImportList(sRTI.getRecordTypeInList(CardRecordList));
            var RTO = new RecordTypeOut();
            RTO.ImportList(sRTO.getRecordTypeOutList(CardRecordList));


            foreach (Classes.CardRecord cardRecord in CardRecordList)
            {
                var cardRecord1 = new CardRecord
                                      {
                                          Duration = cardRecord.Duration,
                                          Organisation = cardRecord.Organisation,
                                          Care = cardRecord.Care.ID,
                                          Conditions = cardRecord.Conditions.ID,
                                          RecrodPosition = cardRecord.RecrodPosition.ID,
                                          RecordSubdivision = cardRecord.RecordSubdivision != null ? cardRecord.RecordSubdivision.ID: 0,
                                          TypeIn = cardRecord.TypeIn.ID,
                                          OrderIn = cardRecord.OrderIn,
                                          RecordTypeOut = cardRecord.TypeOut != null ? cardRecord.TypeOut.ID: (int?) null,
                                          OrderOut = cardRecord.OrderOut,
                                          DateBegin = cardRecord.DateBegin,
                                          DateEnd = cardRecord.DateEnd,
                                          RecordRegime = cardRecord.RecordRegime.ID,
                                          RecordMilitary = cardRecord.RecordMilitary.ID,
                                          Wage = cardRecord.Wage,
                                          RecordPositionType = cardRecord.RecordPositionType.ID,
                                          RecordPost = cardRecord.RecordPost.ID,
                                          Unit = cardRecord.Unit,
                                          Employee = cardRecord.Employee.ID
                                      };

                IEnumerable<CardRecord> query = (from cr in mdb.CardRecord
                                                 where cr.Employee == cardRecord.Employee.ID
                                                 select cr
                                                ).ToList();
                mdb.CardRecord.DeleteAllOnSubmit(query);
                mdb.CardRecord.InsertOnSubmit(cardRecord1);
            }
            mdb.SubmitChanges();
        }
    }
}